/*
 *所有关于zm_message类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*zm_messageService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using Dapper;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using VOL.AppManager.IRepositories;
using VOL.Core.Extensions;
using VOL.Core.ManageUser;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using VOL.Entity.ResDto;
using VOL.Entity.新文件夹;

namespace VOL.AppManager.Services
{
    public partial class zm_messageService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Izm_messageRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public zm_messageService(
            Izm_messageRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        WebResponseContent webResponse = new WebResponseContent();


        public PageGridData<zmmessagedto> GetData(zmmessageQuery options)
        {
            DynamicParameters sqlParams = new DynamicParameters();
            string sql = "    select  * from zm_message where 1=1 ";
            string sqlcount = "    select count(1)  from zm_message where 1=1 ";
            if (!string.IsNullOrEmpty(options.isread))
            {

                sqlcount += " and isread='" + options.isread + "'";
                sql += " and isread='" + options.isread + "'";
            }
            if (!string.IsNullOrEmpty(options.xzqdm))
            {

                sqlcount += " and xzqdm='" + options.xzqdm + "'";
                sql += " and xzqdm='" + options.xzqdm + "'";
            }
            if (!string.IsNullOrEmpty(options.citydm))
            {
                sqlcount += " and citydm='" + options.citydm + "'";
                sql += " and citydm='" + options.citydm + "'";
            }
            if (!string.IsNullOrEmpty(options.provcode))
            {

                sqlcount += " and provdm='" + options.provcode + "'";
                sql += " and provdm='" + options.provcode + "'";
            }

            if (!string.IsNullOrEmpty(options.userid))
            {
                sqlcount += " and userid='" + options.userid + "'";
                sql += " and userid='" + options.userid + "'";
            }

            sql += " order by create_time desc  limit " + options.pagesize + " offset (" + options.pageindex + "-1)*  " + options.pagesize;
            var datacount = repository.DapperContext.ExecuteScalar(sqlcount, sqlParams).GetInt();
            var data = repository.DapperContext.QueryList<zmmessagedto>(sql, sqlParams).ToList();
            int totalPages = datacount / options.pagesize;
            if (datacount % options.pagesize != 0)
            {
                totalPages++;
            }
            return new PageGridData<zmmessagedto> { rows = data, status = 0, total = datacount, summary = new { pagecount = totalPages } };
        }


        public WebResponseContent updateIsRead(zmmessageQuery options)
        {
            string sql = "update zm_message set isread='" + options.isread + "' where id='" + options.id + "'";

            int c = _repository.DapperContext.ExcuteNonQuery(sql, "", null, false);
            if (c > 0)
            {

                return webResponse.OK("已读！", UserContext.Current.RoleId);
            }

            return webResponse.OK("操作失败！");
        }

    }
}
